import log from "electron-log";
import fs from "fs";
import path from "path";
import os from "os";
const homedir = os.homedir();
import arduinoversion from "../arduino.json";
function fond() {
  const apppack = path.join(homedir, "./.hterobotapp/");
  if (!fs.existsSync(apppack)) {
    fs.mkdirSync(apppack);
  }

  const arduino = path.join(apppack, "./Arduino");
  const arduino_dubug = path.join(apppack, "./Arduino/arduino_debug.exe");
  const logpath = path.join(apppack, "log/");
  const text = path.join(apppack, "ino/");
  const textIno = path.join(apppack, "ino/text.ino");
  const build_path = path.join(apppack, "cache");
  const jiance = path.join(apppack, "arduino.json");

  if (!fs.existsSync(logpath)) {
    fs.mkdirSync(logpath);
  }
  if (!fs.existsSync(text)) {
    fs.mkdirSync(text);
  }
  if (!fs.existsSync(textIno)) {
    fs.appendFileSync(textIno, "");
  }
  if (!fs.existsSync(build_path)) {
    fs.mkdirSync(build_path);
  }

  if (fs.existsSync(jiance)) {
    let v = fs.readFileSync(jiance);
    if (v != arduinoversion.version) {
      fs.writeFileSync(jiance, arduinoversion.version);
      return false;
    }
  } else {
    fs.appendFileSync(jiance, arduinoversion.version);
    return false;
  }

  log.transports.file.level = "warn";
  log.transports.file.format = "{h}:{i}:{s}:{ms} {text}";
  // Set approximate maximum log size in bytes. When it exceeds,
  // the archived log will be saved as the log.old.log file
  log.transports.file.maxSize = 5 * 1024 * 1024;
  // Write to this file, must be set before first logging
  if (!fs.existsSync(path.join(logpath + "log.txt"))) {
    fs.appendFileSync(path.join(logpath + "log.txt"), "");
  }
  log.transports.file.file = path.join(logpath + "log.txt");
  // fs.createWriteStream options, must be set before first logging
  // you can find more information at
  // https://nodejs.org/api/fs.html#fs_fs_createwritestream_path_options
  log.transports.file.streamConfig = { flags: "w" };
  // set existed file stream
  log.transports.file.stream = fs.createWriteStream(
    path.join(logpath + "log.txt")
  );
  if (process.platform === "darwin") {
    if (fs.existsSync(path.join(apppack, "./Arduino.app"))) {
      return true;
    } else {
      return false;
    }
  } else if (process.platform === "win32") {
    if (fs.existsSync(arduino) && fs.existsSync(arduino_dubug)) {
      return true;
    } else {
      return false;
    }
  }
}

export default fond;
